<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>药品销售管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />

</head>
<body class="childrenBody">
<!-- 查询条件开始 -->
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">患者</label>
                <div class="layui-input-inline">
                    <select name="patientid" id="search_patientid" lay-search="">
                        <option value="0">请选择患者名称</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="search_providerid" lay-search="">
                        <option value="0">请选择商品供应商</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">药品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="search_goodsid" lay-search="">
                        <option value="0">请选择药品</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">药单id</label>
                <div class="layui-input-inline">
                    <input type="text" name="goodslistnum"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">出库人员</label>
                <div class="layui-input-inline">
                    <input type="text" name="outperson"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="startTime" readonly="readonly"  id="startTime" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="text"  name="endTime" readonly="readonly"  id="endTime"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
            </div>
        </div>
    </form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="salesTable" lay-filter="salesTable"></table>
    <div id="salesToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加药品销售</button>
        <button type="button" lay-event="pichuku" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>批量出库</button>
    </div>

    <div id="salesRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="back" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>退货</button>
    </div>
</div>

<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 10px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">患者</label>
                <div class="layui-input-inline">
                    <select name="patientid" id="patientid"   lay-filter="patientid" lay-search="">
                        <option value="0">请选择患者</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">药品供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="providerid"   lay-filter="providerid" lay-search="">
                        <option value="0">请选择药品供应商</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">药品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid" lay-filter="goodsid" lay-search="">
                        <option value="0">请选择药品</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">销售数量</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="id">
                        <input type="hidden" name="addcurrentnumber" id="addcurrentnumber">
                        <input type="text" class="layui-input"  name="number" lay-verify="required|isNum" placeholder="请输入药品销售包装">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">销售金额</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="sumprice" lay-verify="required|number" placeholder="请输入药品销售规格">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="paytype" value="支付宝" title="支付宝" >
                    <input type="radio" name="paytype" value="微信" title="微信" >
                    <input type="radio" name="paytype" value="信用卡" title="信用卡" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">销售备注</label>
                <div class="layui-input-block">
                    <textarea  name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->


<!-- 退货的弹出层  开始 -->
<div id="backGoodsDiv" style="display: none;padding: 5px">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataBackFrm" lay-filter="dataBackFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">退货数量</label>
            <div class="layui-input-block">
                <input type="hidden" name="id"> <!-- 销售单ID -->
                <input type="hidden" name="currentnumber" id="currentnumber"> <!-- 当前销售单的数量 -->
                <input type="text" class="layui-input"  name="number" lay-verify="required|checknumber" placeholder="请输入退货数量">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货备注</label>
            <div class="layui-input-block">
                <textarea  name="remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doBackSubmit" id="doBackSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 退货的弹出层  结束 -->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    var tableIns;
    layui.use(['jquery','form','table','layer','laydate'],function(){
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var laydate=layui.laydate;
        //渲染时间选择器
        laydate.render({
            elem:'#startTime',
            type:'datetime'
        });
        laydate.render({
            elem:'#endTime',
            type:'datetime'
        });

        //加载 数据
        tableIns=table.render({
            elem: '#salesTable'
            ,url:'/sales/loadAllSales'
            ,toolbar: '#salesToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '药品销售数据表'
            ,height:'full-120'
            ,page: true
            ,cols: [ [
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'销售ID',align:'center',hide:true}
                ,{field:'patientname', title:'患者名称',align:'center',width:'100'}
                ,{field:'goods', title:'药品名称',align:'center',width:'120',templet:function (res) {
                        return '<span>'+res.goods.goodsname+'</span>';}}
                ,{field:'goods', title:'药品规格',align:'center',width:'120',templet:function (res) {
                    return '<span>'+res.goods.size+'</span>';}}
                ,{field:'providername', title:'药品供应商',align:'center',width:'120'}
               // ,{field:'providerid',hide:true, title:'药品供应商id',align:'center',width:'120'}
                ,{field:'salestime', title:'销售时间',align:'center',width:'120'}
                ,{field:'goods', title:'单价',align:'center',width:'60',templet:function (res) {
                        return '<span>'+res.goods.price+'</span>';}}
                ,{field:'number', title:'销售数量',align:'center',width:'80'}
                ,{field:'sumprice', title:'金额',align:'center',width:'80'}
                ,{field:'day', title:'开药天数',align:'center',width:'80'}
                ,{field:'goodslistnum', title:'药单id',align:'center',width:'100'}
                ,{field:'ispay', title:'是否缴费',align:'center',width:'100',templet:function(d){
                        return d.ispay==1?'<font color=red>未缴费</font>':'<font color=blue>已缴费</font>';
                    }}
                ,{field:'isout', title:'是否出库',align:'center',width:'100',templet:function(d){
                        return d.isout==1?'<font color=blue>已出库</font>':'<font color=red>未出库</font>';
                    }}
                ,{field:'outperson', title:'出库人员',align:'center',width:'100'}
                ,{fixed: 'right', title:'操作', toolbar: '#salesRowBar',align:'center',width:'250'}
            ] ]
            ,done: function(res, curr, count){ //处理删除某一页最后一条数据的BUG
                if(res.data.length===0 &&curr!==1){
                    tableIns.reload({
                        page:{
                            curr:(curr-1)
                        }
                    });
                }
            }
        });

        //加载查询条件患者的下拉列表
        $.get("/patient/loadAllPatient",function(res){
            var data=res.data;
            var dom=$("#search_patientid");
            var html='<option value="0">请选择患者</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.patientname+'***'+item.idcardnum.substring(14,18)+'</option>'
            });
            dom.html(html);
            form.render("select");
        });
        //加载查询条件供应商的下拉列表
        $.get("/provider/loadAllProviderForSelect",function(res){
            var data=res.data;
            var dom=$("#search_providerid");
            var html='<option value="0">请选择供应商</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.providername+'</option>'
            });
            dom.html(html);
            form.render("select");
        });

        //加载查询条件药品的下拉列表
        $.get("/goods/loadAllGoodsForSelect",function(res){
            var data=res.data;
            var dom=$("#search_goodsid");
            var html='<option value="0">请选择药品</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'['+item.providername+']</option>'
            });
            dom.html(html);
            form.render("select");
        });

        //模糊查询提交
        form.on("submit(doSearch)",function(data){
          //  alert(data.field.providerid);
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });

        //监听工具条的事件
        table.on("toolbar(salesTable)",function(obj){
            switch(obj.event){
                case 'add':
                    openAddLayer();
                    break;
                case 'pichuku'://监听批量删除工具条按钮
                    pichuku();
                    break;
            };
        });

        //监听行工具条的事件
        table.on("tool(salesTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'update':
                    openUpdateSalesLayer(data);
                    break;
                case 'delete':
                    deleteSales(data);
                    break;
                case 'back':
                    back(data);
                    break;
            };
        });

        var mainIndex;
        var url;
        //打开添加的弹出层
        function openAddLayer(){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['1020px','450px'],
                title:'添加药品销售',
                success:function(){
                    $("#dataFrm")[0].reset();
                    url="/sales/addSales";
                    initProviderSelect();
                    initPatientSelect();
                    initGoodsSelect(0,0);
                    $(".mydiv").hide();
                }
            });
        }
        //批量修改订单为出库状态
        function pichuku() {
            //得到选中行
            var checkStatus = table.checkStatus('salesTable');
            var dataLength=checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定要出库这些药品吗?', {icon: 3, title:'提示'}, function(index){
                    var data=checkStatus.data; //获取选中行的数据
                    var ids="";
                    $.each(data,function(index,item){
                        if(index==0){
                            ids+="ids="+item.id;
                        }else{
                            ids+="&ids="+item.id;
                        }
                    });
                    alert(ids);
                    $.post("/sales/batchUpdateSalesIsOut",ids,function(res){
                        if(res.code==200){
                            tableIns.reload();
                        }
                        layer.msg(res.msg);
                    });
                    layer.close(index);
                });
            }else{
                layer.msg("请选中药品进行操作")
            }
        }

        //初始化添加和修改页面的供应商下拉列表
        function initProviderSelect(providerid){
            var dom=$("#providerid");
            dom.html("");
            $.get("/provider/loadAllProviderForSelect",function(res){
                var data=res.data;
                var dom=$("#providerid");
                var html='<option value="0">请选择供应商</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.providername+'</option>'
                });
                dom.html(html);
                //如果providerid有值就反选
                if(providerid!=undefined){
                    dom.val(providerid);
                }
                form.render("select");
            });
        }
        //加载患者列表
        function initPatientSelect(patientid){
            var dom=$("#patientid");
            dom.html("");
            $.get("/patient/loadAllPatient",function(res){
                var data=res.data;
                var dom=$("#patientid");
                var html='<option value="0">请选择患者</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.patientname+'</option>'
                });
                dom.html(html);
                //如果patientid有值就反选
                if(patientid!=undefined){
                    dom.val(patientid);
                }
                form.render("select");
            });
        }
        //监听添加页面的供应商下拉框的改变事件
        form.on('select(providerid)', function(data){
            var providerid=data.value;
            initGoodsSelect(providerid);
        });

        //加载药品下拉列表
        function initGoodsSelect(providerid,goodsid){
            $.get("/goods/loadGoodsByProviderId",{providerid:providerid},function(res){
                var data=res.data;
                var dom=$("#goodsid");
                dom.html("");
                var html='<option value="0">请选择药品</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'['+item.providername+']</option>'
                });
                dom.html(html);
                //如果goodsid有值就反选
                if(goodsid!==undefined){
                    dom.val(goodsid);
                }
                form.render("select");
            });
        }



        //打开修改的弹出层
        function openUpdateSalesLayer(data){
           // alert(data.providerid);
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','450px'],
                title:'修改药品销售',
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    initPatientSelect(data.patientid);
                    initProviderSelect(data.providerid);
                    initGoodsSelect(data.providerid,data.goodsid);
                    url="/sales/updateSales";
                    //禁用供应商和药品的下拉框
                    $(".mydiv").show();
                }
            });
        }

        //自定义验证,验证销售数量是否大于库存或者少于0
        form.verify({
            isNum: function (value) {
                var goodsid=$("#goodsid").val();
                var goodsNum = '';
                $.ajax({
                    async: false, //改为同步请求
                    url: '/goods/getGoodsByGid',
                    data: {id:goodsid},
                    dataType: 'json',
                    type: 'get',
                    success: function(res) {
                        //alert(res.data.number);
                        goodsNum = res.data.number;
                    }
                });
                if(parseInt(value) < 0 || parseInt(value) > goodsNum){
                    return '销售的药品数量在【1】-【'+goodsNum+"】数量之间";
                }

            }
        });

        //修改或增加提交
        form.on("submit(doSubmit)",function(data){
           // alert(JSON.stringify(data.field));//显示提交的数据
            $.post(url,data.field,function(res){
                if(res.code===200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            return false;
        });

        //删除
        function deleteSales(data){
            layer.confirm('你确定要删除这个药品销售吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/sales/deleteSales",{id:data.id},function(res){
                    if(res.code===200){
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }

        //打开退货的弹出层
        function back(data){
            mainIndex=layer.open({
                type:1,
                content:$("#backGoodsDiv"),
                area:['800px','500px'],
                title:'药品退货',
                success:function(){
                    $("#dataBackFrm")[0].reset();
                    //装载新的数据
                    form.val("dataBackFrm",{id:data.id,currentnumber:data.number});
                }
            });
        }

        //自定义验证规则(验证退货的数量)
        form.verify({
            checknumber: function(value){
                var currentnumber=parseInt($("#currentnumber").val());
                if(parseInt(value)>currentnumber||parseInt(value)<1){
                    return '退货数量只能在【0】-【'+currentnumber+"】数量之间";
                }
            }
        });

        //退货提交
        form.on("submit(doBackSubmit)",function(data){
            //alert(JSON.stringify(data.field));
            $.post("/salesback/addSalesback",data.field,function(res){
                if(res.code===200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            return false;
        });
    });
</script>

</body>
</html>